package cn.com.qiancheng.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.com.qiancheng.dao.UnFinishDao;
import cn.com.qiancheng.dto.Evaluation;
import cn.com.qiancheng.dto.Student;
import cn.com.qiancheng.service.ResultsService;

/**
 * Servlet implementation class Results
 */
public class ResultsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int stuId = Integer.parseInt(request.getParameter("stuId"));
		int evaId = Integer.parseInt(request.getParameter("evaId"));
		List<Integer> qid = new ArrayList<>();
		List<Integer> anid = new ArrayList<>();
		Enumeration<String> names = request.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			if (name.equals("stuId") || name.equals("evaId")) {
				continue;
			}
			qid.add(Integer.parseInt(name));
			anid.add(Integer.parseInt(request.getParameter(name)));

		}
		ResultsService rs = new ResultsService();
		int t = rs.insertResults(stuId, evaId, qid, anid);
		if (t == 0) {

		} else {
			HttpSession session = request.getSession();
			List<Evaluation> list = null;
			Student student = (Student) session.getAttribute("stu");
			list = new UnFinishDao().searchUnFinishDao(student);
			session.setAttribute("eva", list);
			RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/JSP/main.jsp");
			rd.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
